<?php

namespace api\modules\v1\forms\buss;

use common\helpers\RedisHelper;
use common\models\base\Project;
use common\models\buss\ToolWater;
use common\models\sys\SysFile;
use services\member\MemberService;
use services\sys\SysFileService;
use Yii;
use yii\base\Model;


class ToolWaterForm extends Model
{
    /**
     * @var int $id ID
     * @var int $project_id 项目ID
     * @var string $type 类型【1文字、2图片】
     * @var string $text 文字
     * @var string $file 文件名
     * @var string $alpha 透明度【单位：%】
     */
    public $id;
    public $project_id;
    public $type;
    public $text;
    public $file;
    public $alpha;

    /**
     * @var ToolWater $_toolWaterConfig
     * @var Project $_project
     * @var SysFile $_file
     */
    protected $_toolWater;
    protected $_project;
    protected $_file;
    protected $_haveProjectList;// 权限下项目列表


    public function rules()
    {
        return [
            // 参数验证
            [['project_id', 'type', 'alpha'], 'required', 'message' => '{attribute}不能为空', 'on' => ['create']],
            [['id', 'project_id', 'type', 'alpha'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],
            [['id'], 'required', 'message' => '{attribute}不能为空', 'on' => ['delete']],

            [['id'], 'checkId', 'skipOnEmpty' => false, 'on' => ['edit', 'delete']],
            [['project_id'], 'checkProjectId', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['type'], 'checkType', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['text'], 'checkText', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['file'], 'checkFile', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['alpha'], 'checkAlpha', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 新增
        $scenarios['create'] = ['project_id', 'type', 'text', 'file', 'alpha'];
        // 编辑
        $scenarios['edit'] = ['id', 'project_id', 'type', 'text', 'file', 'alpha'];
        // 删除
        $scenarios['delete'] = ['id'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'project_id' => '项目ID',
            'type' => '类型',
            'text' => '内容',
            'file' => '图片',
            'alpha' => '透明度',
        ];
    }
    /**
     * 检验水印ID
     * @param $attribute
     * @return void
     */
    public function checkId($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        $toolWaterConfig = $this->getToolWater();
        if (!$toolWaterConfig) {
            $this->addError($attribute, '水印不存在');
            return;
        }

        if ($toolWaterConfig->status == ToolWater::STATUS_DELETE) {
            $this->addError($attribute, '水印已删除');
            return;
        }
    }

    /**
     * 检验项目ID
     * @param $attribute
     * @return void
     */
    public function checkProjectId($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }

        $project = $this->getProject();
        if (empty($project)) {
            $this->addError($attribute, '项目不存在');
            return;
        }
        if ($project->status == Project::STATUS_DELETE) {
            $this->addError($attribute, '项目已删除');
            return;
        }
        if ($this->getScenario() == 'edit') {
            $tool = $this->getToolWater();
            if ($this->project_id == $tool->project_id) {// 未修改项目则不检验
                return;
            }
        }
        if (in_array($this->project_id, array_column($this->getHaveProjectList(), 'value')) === false) {
            $this->addError($attribute, '无权限选择当前项目');
            return;
        }
        if ($this->getScenario() == 'edit' && $this->getToolWater()->project_id == $this->project_id) {
            return;
        }
        // 判断项目是否存在配置
        $exist = ToolWater::findOne([
            'project_id' => $this->project_id,
            'status' => ToolWater::STATUS_ENABLE
        ]);
        if ($exist) {
            $this->addError($attribute, "{$project->project_name}项目已配置水印");
            return;
        }
    }

    /**
     * 校验类型
     * @param $attribute
     * @return void
     */
    public function checkType($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        $types = ToolWater::$types;
        if (isset($types[$this->type]) == false) {
            $this->addError($attribute, '类型错误');
            return;
        }
    }

    /**
     * 校验文字
     * @param $attribute
     * @return void
     */
    public function checkText($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->type == ToolWater::TYPE_FILE) {
            return;
        }
        if (empty($this->text)) {
            $this->addError($attribute, '内容不能为空');
            return;
        }
        $len = mb_strlen($this->text);
        if ($len > 64) {
            $this->addError($attribute, '内容过长，最多64个字');
            return;
        }
    }

    /**
     * 校验文件
     * @param $attribute
     * @return void
     */
    public function checkFile($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->type == ToolWater::TYPE_TEXT) {
            return;
        }
        if (empty($this->file)) {
            $this->addError($attribute, '请上传图片');
            return;
        }
        $file = $this->getFile();
        if (empty($file)) {
            $this->addError($attribute, '图片不存在');
            return;
        }
    }

    /**
     * 校验透明度
     * @param $attribute
     * @return void
     */
    public function checkAlpha($attribute): void
    {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->alpha < 20 || $this->alpha > 80) {
            $this->addError($attribute, '透明度超出范围，20-80区间');
            return;
        }
    }

    /**
     * 获取水印信息
     * @return ToolWater|null
     */
    public function getToolWater(): ?ToolWater
    {
        if (empty($this->_toolWater)) {
            $this->_toolWater = ToolWater::findOne($this->id);
        }
        return $this->_toolWater;
    }

    /**
     * 获取项目信息
     * @return Project|null
     */
    public function getProject(): ?Project
    {
        if (empty($this->_project)) {
            $this->_project = Project::findOne($this->project_id);
        }

        return $this->_project;
    }

    /**
     * 获取文件
     * @return SysFile|null
     */
    public function getFile(): ?SysFile
    {
        if (empty($this->_file)) {
            $this->_file = SysFileService::getSysFile(SysFile::SITE_TOOL, SysFile::MODULE_TOOL_WATER, $this->file);
        }
        return $this->_file;
    }

    /**
     * 获取账号信息
     * @return Project[]
     */
    public function getHaveProjectList(): array
    {
        if (empty($this->_haveProjectList)) {
            $this->_haveProjectList = MemberService::getProjectEditCondition();
        }

        return $this->_haveProjectList;
    }
}
